import 'dart:async';

import 'package:flutter_clx_base/api/base_dio.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import '../constants.dart';
import 'dio_utils.dart';
import 'http_api.dart';

export 'dio_utils.dart';
export 'http_api.dart';

class NetUtils {
  static Future<void> get(
    url, {
    required queryParameters,
    NetSuccessCallback? successCallback,
    NetSuccessListCallback? successListCallback,
    NetErrorCallback? errorCallback,
    Options? options,
  }) async {
    DioUtils.instance.request(
        requestUrl: url,
        queryParameters: queryParameters,
        method: Method.get,
        successCallback: (data) {
          successListCallback?.call(data) ?? successCallback?.call(data);
        },
        errorCallback: (code, msg) {
          EasyLoading.dismiss();
          errorCallback != null
              ? errorCallback.call(code, msg)
              : ToastUtil.showToast(msg);
        },
        options: options);
  }

  static Future<void> getAsync(
      url, {
        required queryParameters,
        NetSuccessCallback? successCallback,
        NetSuccessListCallback? successListCallback,
        NetErrorCallback? errorCallback,
        Options? options,
      }) async {
    Completer completer = Completer();
    DioUtils.instance.request(
        requestUrl: url,
        queryParameters: queryParameters,
        method: Method.get,
        successCallback: (data) {
          completer.complete();
          successListCallback?.call(data) ?? successCallback?.call(data);
        },
        errorCallback: (code, msg) {
          completer.complete();
          EasyLoading.dismiss();
          errorCallback != null
              ? errorCallback.call(code, msg)
              : ToastUtil.showToast(msg);
        },
        options: options);
    return completer.future;
  }

  static Future<void> post(
    url, {
    required data,
    dynamic queryParameters,
    NetSuccessCallback? successCallback,
    NetSuccessListCallback? successListCallback,
    NetErrorCallback? errorCallback,
    Options? options,
  }) async {
    DioUtils.instance.request(
        requestUrl: url,
        method: Method.post,
        data: data,
        queryParameters: queryParameters,
        successCallback: (data) {
          successListCallback?.call(data) ?? successCallback?.call(data);
        },
        errorCallback: (code, msg) {
          EasyLoading.dismiss();
          errorCallback != null
              ? errorCallback.call(code, msg)
              : ToastUtil.showToast(msg);
        },
        options: options);
  }

  static Future<void> postAsync(
    url, {
    required data,
    dynamic queryParameters,
    NetSuccessCallback? successCallback,
    NetSuccessListCallback? successListCallback,
    NetErrorCallback? errorCallback,
    Options? options,
  }) async {
    Completer completer = Completer();
    DioUtils.instance.request(
        requestUrl: url,
        method: Method.post,
        data: data,
        queryParameters: queryParameters,
        successCallback: (data) {
          completer.complete();
          successListCallback?.call(data) ?? successCallback?.call(data);
        },
        errorCallback: (code, msg) {
          completer.complete();
          EasyLoading.dismiss();
          errorCallback != null
              ? errorCallback.call(code, msg)
              : ToastUtil.showToast(msg);
        },
        options: options);
    return completer.future;
  }

  static Future<void> put(
    url, {
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? data,
    required NetSuccessCallback successCallback,
    NetErrorCallback? errorCallback,
    Options? options,
  }) async {
    DioUtils.instance.request(
        requestUrl: url,
        method: Method.put,
        queryParameters: queryParameters,
        data: data,
        successCallback: successCallback,
        errorCallback: (code, msg) {
          EasyLoading.dismiss();
          errorCallback != null
              ? errorCallback.call(code, msg)
              : ToastUtil.showToast(msg);
        },
        options: options);
  }

  static Future<void> delete(
    url, {
    Map<String, dynamic>? queryParameters,
    Map<String, dynamic>? data,
    required NetSuccessCallback successCallback,
    NetErrorCallback? errorCallback,
    Options? options,
  }) async {
    DioUtils.instance.request(
        requestUrl: url,
        method: Method.delete,
        queryParameters: queryParameters,
        data: data,
        successCallback: successCallback,
        errorCallback: (code, msg) {
          EasyLoading.dismiss();
          errorCallback != null
              ? errorCallback.call(code, msg)
              : ToastUtil.showToast(msg);
        },
        options: options);
  }

  // 上传图片到服务
  static Future<String> uploadImage({
    required String path,
  }) async {
    String imageUrl = '';

    var formData = FormData.fromMap({
      'file': await MultipartFile.fromFile(path, filename: 'image.png'),
      'dir': 'carrier-driver',
    });

    var data = await DioUtils.instance.requestSync(
        requestUrl: ServiceApi.uploadImage,
        method: Method.post,
        data: formData);

    if (data != null) {
      String url = data['data'];
      imageUrl = url;
    } else {
      ToastUtil.showToast("图片上传失败，请联系公司管理员或客服");
    }

    return imageUrl;
  }

  static Future<void> getDomainHost() async {
    var data = await DioUtils.instance
        .requestSync(requestUrl: ServiceApi.getDomainHost, method: Method.get);
    if (data != null && data['code'] == 0) {
      String url = data['data'];
      SpUtil.putString(Constants.domain, url);
      ImageWidget.domain = url;
    } else {
      ToastUtil.showToast(data['msg']);
    }
  }
}
